/**
 * @param {number[]} tickets
 * @param {number} k
 * @return {number}
 */
var timeRequiredToBuy = function(tickets, k) {
  let time = 0;
  function buyTickOneTime() {
    for(let i = 0; i < tickets.length; i++) {
      if(tickets[i] != 0) {
        tickets[i]--;
        time++;
      }
      if(tickets[k] == 0) {
        return time;
      }
    }
    return -1;
  }
  let max = Math.max(...tickets);
  while(max) {
    let time = buyTickOneTime();
    if(time != -1) return time;
    max = Math.max(...tickets);
  }
};
let tickets = [2,3,2], k = 2;
console.log(timeRequiredToBuy(tickets, k));
tickets = [5,1,1,1], k = 0;
console.log(timeRequiredToBuy(tickets, k));
